/*
 * @(#) $Header$
 *
 * Copyright (C) 2006  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.baselib.util.iterator;

import java.util.Iterator;
import java.util.Collection;
import java.util.ListIterator;
import ca.forklabs.baselib.util.Algorithm;
import ca.forklabs.baselib.util.iterator.Resources;

/**
 * Class {@code BackInsertIterator} is a <em>write-only</em> iterator that
 * inserts elements in the adapter collection using the method
 * {@link Collection#add(Object)}
 * <p>
 * This class is based on
 * <a href="http://www.sgi.com/tech/stl/back_insert_iterator.html">C++ {@code back_insert_iterator}</a>.
 *
 * @param   <E>   the type of the elements.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.baselib.util.iterator.BackInsertIterator">Daniel Léonard</a>
 * @version $Revision$
 * @see   Algorithm#copy(Iterator, ListIterator)
 */
public class BackInsertIterator<E> implements ListIterator<E> {

//---------------------------
// Instance variables
//---------------------------

   /** The adapted list. */
   private Collection<E> the_collection;


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    * @param   collection   the adapted list.
    * @exception   NullPointerException   if the list is {@code null}.
    */
   public BackInsertIterator(Collection<E> collection) {
      this.setCollection(collection);
      }


//---------------------------
// Accessors and mutators
//---------------------------

   /**
    * Gets the adapted collection.
    * @return   the collection.
    */
   protected Collection<E> getCollection() {
      return this.the_collection;
      }

   /**
    * Changes the adapted collection.
    * @param   collection   the new collection.
    * @exception   NullPointerException   if the collection is {@code null}.
    */
   protected void setCollection(Collection<E> collection) {
      if (null == collection) {
         String message = this.getCannotAdaptErrorMessage();
         throw new NullPointerException(message);
         }
      this.the_collection = collection;
      }


//---------------------------
// Implemented methods from java.util.ListIterator
//---------------------------

   /**
    * The iterator always have another element.
    * @return   always {@code true}.
    * @see   #next()
    */
   @Override
   public boolean hasNext() {
      boolean has_next = true;
      return has_next;
      }

   /**
    * Adds the given element at the end of adapted collection.
    * @param   element   the element to add.
    */
   @Override
   public void set(E element) {
      Collection<E> collection = this.getCollection();
      collection.add(element);
      }

   /**
    * Always return {@code null}.
    * @return   always {@code null}.
    * @see   #hasNext()
    */
   @Override
   public E next() {
   // does nothing
      return null;
      }

   /**
    * Unsupported operation.
    * @param   element   unused.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public void add(E element) {
      String message = this.getUnsupportedMethodErrorMessage("add(E)");
      throw new UnsupportedOperationException(message);
      }

   /**
    * Unsupported operation.
    * @return   never returns.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public boolean hasPrevious() {
      String message = this.getUnsupportedMethodErrorMessage("hasPrevious()");
      throw new UnsupportedOperationException(message);
      }

   /**
    * Unsupported operation.
    * @return   never returns.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public int nextIndex() {
      String message = this.getUnsupportedMethodErrorMessage("nextIndex()");
      throw new UnsupportedOperationException(message);
      }

   /**
    * Unsupported operation.
    * @return   never returns.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public E previous() {
      String message = this.getUnsupportedMethodErrorMessage("previous()");
      throw new UnsupportedOperationException(message);
      }

   /**
    * Unsupported operation.
    * @return   never returns.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public int previousIndex() {
      String message = this.getUnsupportedMethodErrorMessage("previousIndex()");
      throw new UnsupportedOperationException(message);
      }

   /**
    * Unsupported operation.
    * @exception   UnsupportedOperationException   always.
    */
   @Override
   @SuppressWarnings("nls")
   public void remove() {
      String message = this.getUnsupportedMethodErrorMessage("remove()");
      throw new UnsupportedOperationException(message);
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Gets the error message telling that the underlying list is {@code null}.
    * @return   the formatted message.
    */
   protected String getCannotAdaptErrorMessage() {
      String key = Resources.BACK_INSERTER_ITERATOR_CANNOT_ADAPT;
      String message = Resources.getLocalizedString(key);
      return message;
      }

   /**
    * Gets the error message telling that the given method is unsupported.
    * @param   method   the name of the unsupported method.
    * @return   the formatted message.
    */
   protected String getUnsupportedMethodErrorMessage(String method) {
      String key = Resources.ITERATOR_UNSUPPORTED_METHOD;
      String message = Resources.getLocalizedString(key, method);
      return message;
      }

   }
